import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document, Types } from 'mongoose';

export type UserDocument = User & Document;

type user_id = string;

export interface UserInfo {
  nickname: string;
}
export interface ChatData {
  user_id: Types.ObjectId;
  records: Array<RecordsItem>;
}

export interface RecordsItem {
  id: Types.ObjectId;
  user_id: Types.ObjectId;
  text: string;
  mine: boolean;
  time: string;
}

export interface AddressBookItem {
  id: Types.ObjectId;
  nickname: string;
}

export enum TaskType {
  // 添加好友
  ADD_FRIEND = 0,
  // A添加B，向B发送了type=0的任务，等B收到该任务，拒绝/同意后向A发送一个type=1的任务
  DEAL_WITH_ADD_FRIEND = 1,
}

export interface TaskItem {
  id: Types.ObjectId;
  type: TaskType;
  // 这个属性表示是否同意，可以用在多种需要决定的任务环境中。
  agreed?: boolean;
}

@Schema()
export class User {
  @Prop({
    required: true,
    unique: true,
  })
  username: string;

  @Prop({
    required: true,
  })
  password: string;

  @Prop({
    required: true,
  })
  address_book: Array<AddressBookItem>;

  @Prop({
    required: true,
    type: Object,
  })
  chat_map: Record<user_id, ChatData>;

  @Prop({
    required: true,
    type: Object,
  })
  info: UserInfo;

  @Prop({
    required: true,
  })
  task_stack: Array<TaskItem>;
}

export const UserSchema = SchemaFactory.createForClass(User);
